package com.umpay.service.consum.impl;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.umpay.dao.online.TransDao;
import com.umpay.dao.online.consum.ConsumOrderOnlineDao;
import com.umpay.dict.retcode.MerBusiEC;
import com.umpay.entity.Trans;
import com.umpay.entity.consum.ConsumOrder;
import com.umpay.exception.BusinessException;
import com.umpay.service.consum.ConsumeScancodeService;

@Service("ConsumeScancodeService")
public class ConsumeScancodeServiceImpl implements ConsumeScancodeService {

	private static final Logger log = LogManager.getLogger(ConsumeScancodeServiceImpl.class);
	
	@Autowired
	private ConsumOrderOnlineDao consumOrderOnlineDao;
	
	@Autowired
	private TransDao consumTransOnlineDao;
	
	@Override
	public ConsumOrder find(ConsumOrder order) {
		log.info("【查询消费订单表信息】"+order);
		ConsumOrder consumOrder = consumOrderOnlineDao.find(order);
		log.info("【查询消费订单表信息】"+consumOrder);
		return consumOrder;
	}
	
	@Override
	@Transactional
	public void updateConsumOrderAndInertTrans(ConsumOrder order,Trans consumeTrans) {
		int orderResult = -1;
		int transResult = -1;
		log.info("【更新消费订单、插入交易表信息开始】");
		try {
			log.info(String.format("更新消费订单信息,:[%s]",order));
			log.info(String.format("插入消费交易信息,:[%s]",consumeTrans));
			orderResult = consumOrderOnlineDao.update(order);
			transResult=consumTransOnlineDao.insert(consumeTrans);
		} catch (DataAccessException e) {
            if (e instanceof DuplicateKeyException) {
                log.error(String.format("【交易表】插入主键冲突,插入交易信息[%s]",consumeTrans));
                throw new BusinessException(MerBusiEC.DB_DUPLICATE_KEY,"商户支付流水重复");
            }
			log.error("【更新消费订单、插入交易表信息失败】失败",e);
			throw new BusinessException(MerBusiEC.DB_ERROR,"更新消费订单、插入交易表信息失败");
		} catch(Exception e){
			log.error("【更新消费订单、插入交易表信息失败】失败",e);
			throw new BusinessException(MerBusiEC.DB_ERROR,"更新消费订单、插入交易表信息失败");
		} 
		if(orderResult != 1){
			throw new BusinessException(MerBusiEC.DB_ERROR,"更新消费订单信息异常");
		}
		if(transResult != 1){
			throw new BusinessException(MerBusiEC.DB_ERROR,"插入消费交易信息异常");
		}
		log.info("【更新消费订单表信息结束】");
	}
	
	@Override
	@Transactional
	public void update(ConsumOrder order, Trans tran) throws BusinessException{
		int orderResult = -1;
		int transResult = -1;
		log.info("【更新扫码支付表信息开始】");
		try {
			log.info(String.format("更新消费订单信息,:[%s]",order));
			log.info(String.format("更新消费交易信息,:[%s]",tran));
			orderResult = consumOrderOnlineDao.update(order);
			transResult = consumTransOnlineDao.update(tran);
		} catch (DataAccessException e) {
			log.error("【更新扫码支付表】更新失败",e);
			throw new BusinessException(MerBusiEC.DB_ERROR,"更新扫码支付表失败");
		} catch(Exception e){
			log.error("【更新扫码支付表】更新失败",e);
			throw new BusinessException(MerBusiEC.DB_ERROR,"更新扫码支付表失败");
		} 
		if(orderResult != 1){
			throw new BusinessException(MerBusiEC.DB_ERROR,"更新消费订单信息异常");
		}
		if(transResult != 1){
			throw new BusinessException(MerBusiEC.DB_ERROR,"更新消费交易信息异常");
		}
		log.info("【更新扫码支付表信息结束】");
	}
	
	public void update(ConsumOrder order) throws BusinessException{
		int result = -1;
		log.info("【更新扫码支付订单表信息开始】");
		try {
			log.info(String.format("更新扫码支付订单表状态及其他信息,:[%s]",order));
			result = consumOrderOnlineDao.update(order);
			log.info(String.format("更新扫码支付订单表结束，结果result=[%s]",result));
		} catch (DataAccessException e) {
			log.error("【更新扫码支付订单表】更新失败",e);
			throw new BusinessException(MerBusiEC.DB_ERROR,"更新在线分账订单表失败");
		} catch(Exception e){
			log.error("【更新扫码支付订单表】更新失败",e);
			throw new BusinessException(MerBusiEC.DB_ERROR,"更新在线分账订单表失败");
		} 
		if(result != 1){
			throw new BusinessException(MerBusiEC.DB_ERROR,"更新在线分账订单表异常");
		}
		log.info("【更新扫码支付订单表信息结束】");
	}
}
